(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:AuthrizeCtrl
     * @description
     * # AuthrizeCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('AuthrizeCtrl', AuthrizeCtrl);

    AuthrizeCtrl.$inject = ['$scope', 'dataService', 'helper', '$state', '$location', 'sessionService'];

    function AuthrizeCtrl($scope, dataService, helper, $state, $location, sessionService) {

        $scope.listAction = function() {

            $scope.page_Size = 10;
            $scope.getlist(1);
        }

        $scope.getlist = function(pageNo) {

            var user_id = $location.search().user_id;

            var query = { page_no: pageNo ? pageNo : "1", page_size: $scope.page_Size, "order_by": "user_id:desc" };
            if ($scope.keyword) {
                query.query = { nick_name: $scope.keyword };
            }
            if (user_id) {
                query.query = { user_id: user_id };
            }

            dataService.sellerGet(query).then(function(list) {
                if (list) {
                    $scope.users = list.users;
                    $scope.total_count = list.total_count;
                }
            });
        }


        //status=1正常，2冻结
        $scope.StatusUpdate = function(status, uid) {
            var api = { "di": "disableUpdate", "en": "enableUpdate" }
            helper.myConfirm('confirm operation', function() {
                var data = { "user_id": uid };
                var type = "di";
                if (status) {
                    type = "en";
                }
                dataService[api[type]](data).then(
                    function(r) {
                        helper.myAlert("success", null, null, function() {
                            $state.go("authrize.list");
                        });
                    }
                );
            });
        }


        //新增接口调用者
        $scope.addAction = function() {
            $scope.api_data = {};

            $scope.api_data.api_id = "";
            $scope.api_secret = "";
            $scope.api_secret_confirm = "";
        }

        $scope.saveData = function() {
            var data = {};
            data.partner_id = sessionService.get('tokenUser').CompanyId;
            data.api_id = $scope.api_data.api_id;
            data.api_secret = $scope.api_data.api_secret;
            data.api_secret_confirm = $scope.api_data.api_secret_confirm;

            dataService.openapiuserAdd(data).then(function(result) {
                if (result.Msg) {
                    helper.myAlert("error：" + result.Msg);
                    return false;
                } else {
                    helper.myAlert("success", null, null, function() {
                        $state.go("authrize.list");
                    });
                }
            });
        }
    }
})();